import { message } from "ant-design-vue";
import axios from "axios";

const request = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_API,
  timeout: 5000,
});

request.interceptors.request.use(
  (config) => {
    // Add X-Access-Token header to every request, you can add other custom headers here
    return config;
  },
  (error) => {
    Promise.reject(error);
  }
);

request.interceptors.response.use(
  (response) => {
    const res = response.data;
    if (response.status !== 200) {
      message.error(res.error.message || "Error");
      return Promise.reject(new Error(res.error.message || "Error"));
    } else {
      return response.data || response;
    }
  },
  (error) => {
    const res = error.response?.data;
    if (res) {
      message.error(res.error.message);
    } else {
      message.error(error.message);
    }
  }
);

export default request;
